BUILD_DIR = ./build
ENTRY_POINT = 0xc0001500
AS = nasm
CC = gcc
LD = ld
BOOT_X86_DIR = boot/x86/
LIB = -I lib/ -I lib/kernel/  -I kernel/
ASFLAGS = -f elf
ASIB = -I include/ -I include/boot/
CFLAGS = -Wall -m32 -fno-stack-protector $(LIB) -c -fno-builtin -W -Wstrict-prototypes -Wmissing-prototypes
LDFLAGS =  -m elf_i386 -Ttext $(ENTRY_POINT) -e main -Map $(BUILD_DIR)/kernel.map
OBJS = $(BUILD_DIR)/main.o $(BUILD_DIR)/print.o
    





# 编译loader和boot \

$(BUILD_DIR)/boot.bin: $(BOOT_X86_DIR)boot.asm
	$(AS) $(ASIB) $< -o $@

$(BUILD_DIR)/loader.bin: $(BOOT_X86_DIR)loader.asm
	$(AS) $(ASIB) $< -o $@

##############    汇编代码编译    ###############


$(BUILD_DIR)/print.o: lib/kernel/print.S
	$(AS) $(ASFLAGS) $< -o $@



##############     c代码编译     ###############
$(BUILD_DIR)/main.o: kernel/main.c lib/kernel/print.h lib/stdint.h
	$(CC) $(CFLAGS) $< -o $@

##############    链接所有目标文件    #############
$(BUILD_DIR)/kernel.bin: $(OBJS)
	$(LD) $(LDFLAGS) $^ -o $@

.PHONY : mk_dir hd clean all

mk_dir:
	if [ ! -d $(BUILD_DIR) ]; then mkdir $(BUILD_DIR); fi

hd:
	dd if=$(BUILD_DIR)/boot.bin of=hd60m.img bs=512 count=1 conv=notrunc
	dd if=$(BUILD_DIR)/loader.bin of=hd60m.img bs=512 count=4 seek=2 conv=notrunc
	dd if=$(BUILD_DIR)/kernel.bin of=hd60m.img bs=512 count=200 seek=9 conv=notrunc


clean:
	rm -rf $(BUILD_DIR)

build: $(BUILD_DIR)/boot.bin $(BUILD_DIR)/loader.bin $(BUILD_DIR)/kernel.bin

all: mk_dir build hd
